package com.gtpickupapp;

import android.app.Activity;
import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.Toast;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TextView;
import java.awt.*;

public class MainPage extends TabActivity implements OnClickListener {
	
	private static Context cont;
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {    
		MenuInflater inflater = getMenuInflater();    
		inflater.inflate(R.menu.mainmenu, menu);
		
		cont = this.getApplicationContext();
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {    
		// Handle item selection    
		switch (item.getItemId()) {    
		case R.id.profile:        
			profile();
			return true;    
		case R.id.logout:        
			logout();
			return true;
		case R.id.home:
			home();
			return true;
		default:        
			return super.onOptionsItemSelected(item);    
		
		}
	}
	
	public void logout(){
		//TODO will probably need to close DB connection and remove users info on logout
		Intent myintent = new Intent(MainPage.this,gtpickupapp.class);
		MainPage.this.startActivity(myintent);
		finish();
	}
	
	public void profile(){
		Bundle b = getIntent().getExtras();
		Bundle bundle = new Bundle();
		bundle.putString("uName", b.getString("uName"));
		Intent myintent = new Intent(MainPage.this,RealProfilePage.class);
		myintent.putExtras(bundle);
		MainPage.this.startActivity(myintent);
		finish();
	}
	
	public void home(){
		//TODO make this return the user to the "home" page with all of the information reset in each tab
		Bundle b = getIntent().getExtras();
		Bundle bundle = new Bundle();
		bundle.putString("uName", b.getString("uName"));
		Intent myintent = new Intent(MainPage.this,MainPage.class);
		myintent.putExtras(bundle);
		MainPage.this.startActivity(myintent);
		finish();
	}
	
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);    
		setContentView(R.layout.homescreen);  
		
		Bundle bundle = getIntent().getExtras();
		
		Resources res = getResources(); 
		// Resource object to get Drawables    
		final TabHost tabHost = getTabHost();  
		// The activity TabHost    
		TabHost.TabSpec spec;  
		// Resusable TabSpec for each tab    
		Intent intent;  
		// Reusable Intent for each tab    
		// Create an Intent to launch an Activity for the tab (to be reused)    
		intent = new Intent().setClass(this, MyEventsGroup.class);    
		intent.putExtras(bundle);
		// Initialize a TabSpec for each tab and add it to the TabHost    
		
		//spec = tabHost.newTabSpec("artists").setIndicator("",res.getDrawable(R.drawable.ic_tab_my)).setContent(intent);  
		spec = tabHost.newTabSpec("my").setIndicator("My Events").setContent(intent);    
		
		tabHost.addTab(spec);    
		// Do the same for the other tabs    
		intent = new Intent().setClass(this, NewEventGroup.class);
		intent.putExtras(bundle);
		//spec = tabHost.newTabSpec("albums").setIndicator("",res.getDrawable(R.drawable.ic_tab_new)).setContent(intent);
		spec = tabHost.newTabSpec("new").setIndicator("New Events").setContent(intent);
		
		tabHost.addTab(spec);    
		Intent intt = new Intent().setClass(this, SearchEventsGroup.class);
		intt.putExtras(bundle);
		
		//spec = tabHost.newTabSpec("songs").setIndicator("",res.getDrawable(R.drawable.ic_tab_search)).setContent(intent);
		spec = tabHost.newTabSpec("search").setIndicator("Search Events").setContent(intt);    
		tabHost.addTab(spec);    
		tabHost.setCurrentTab(0);
		
		
		tabHost.setOnTabChangedListener(new OnTabChangeListener(){
			public void onTabChanged(String tabId) {
				if(tabId == "new"){
					//Toast.makeText(MainPage.this,"Please Select a location" , 1).show();
				}
				//below used to reset the tabs when you change tabs
			
				//LocalActivityManager lm = getLocalActivityManager();
				//lm.destroyActivity(tabId, true);
			}
		});
		/*tabHost.setOnTabChangedListener(new OnTabChangeListener(){
			public void onTabChanged(String tabId) {
				// TODO Auto-generated method stub
				tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#000000")); 
			}
		});*/
		 
		
	
	}
	
	
	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Toast.makeText(MainPage.this,"tt" , 1).show();
	}
	
	public static Context getR(){
		
		return cont;
	}
	
	
}
